package com.smedi.ismedi.inmail.core.port.adapter.restful;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smedi.ismedi.authority.starter.annotation.SkipAuthority;
import com.smedi.ismedi.database.starter.annotation.SkipSqlPermission;
import com.smedi.ismedi.inmail.api.dto.request.QueryListTaskRecordRequest;
import com.smedi.ismedi.inmail.api.dto.request.QueryPageTaskRecordRequest;
import com.smedi.ismedi.inmail.api.dto.request.TaskRecordDeleteRequest;
import com.smedi.ismedi.inmail.api.dto.request.TaskRecordRequest;
import com.smedi.ismedi.inmail.api.dto.response.TaskRecordCountResponse;
import com.smedi.ismedi.inmail.api.dto.response.TaskRecordResponse;
import com.smedi.ismedi.inmail.api.feign.RemoteTaskFeignApi;
import com.smedi.ismedi.inmail.core.application.command.TaskRecordCommand;
import com.smedi.ismedi.inmail.core.application.query.TaskQueryService;
import com.smedi.ismedi.inmail.core.application.query.vo.TaskRecordVo;
import com.smedi.ismedi.inmail.core.application.service.ScheduleApplicationService;
import com.smedi.ismedi.inmail.core.application.service.TaskApplicationService;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.TaskRecord;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@AllArgsConstructor
@EnableScheduling
public class TaskController implements RemoteTaskFeignApi {
    private final DozerUtil dozerUtil;

    private final TaskQueryService taskQueryService;

    private final TaskApplicationService taskApplicationService;

    private final ScheduleApplicationService scheduleApplicationService;

    @Override
    public ResponseData<Boolean> postUpdateTask(TaskRecordRequest taskRecordRequest) {
        return new ResponseData<>().success(taskApplicationService.updateTaskStatus(dozerUtil.convertor(taskRecordRequest, TaskRecordCommand.class)));
    }

    @Override
    public ResponseData<TaskRecordResponse> postAddTask(TaskRecordRequest taskRecordRequest) {
        TaskRecord taskRecord = taskApplicationService.addTask(dozerUtil.convertor(taskRecordRequest, TaskRecordCommand.class));
        return new ResponseData<>().success(dozerUtil.convertor(taskRecord, TaskRecordResponse.class));
    }

    @Override
    public ResponseData<Boolean> postDeleteTask(List<Long> ids) {
        return new ResponseData<>().success(taskApplicationService.deleteTaskList(ids));
    }

    @Override
    public ResponseData<Boolean> postDeleteTaskBySources(List<TaskRecordDeleteRequest> taskRecordDeleteRequests) {
        List<TaskRecordCommand> taskRecordCommands = dozerUtil.convertor(taskRecordDeleteRequests, TaskRecordCommand.class);
        return new ResponseData<>().success(taskApplicationService.deleteTaskListBySources(taskRecordCommands));
    }

    @Override
    public ResponseData<List<TaskRecordResponse>> postReadTask(List<Long> ids) {
        return new ResponseData<>().success(dozerUtil.convertor(taskApplicationService.readTaskList(ids), TaskRecordResponse.class));
    }

    @Override
    public ResponseData<TaskRecordResponse> postReadTaskDaily(TaskRecordRequest taskRecordRequest) {
        TaskRecordCommand taskRecordCommand = dozerUtil.convertor(taskRecordRequest, TaskRecordCommand.class);
        return new ResponseData<>().success(dozerUtil.convertor(taskApplicationService.readTaskDaily(taskRecordCommand), TaskRecordResponse.class));
    }

    @Override
    public ResponseData<TaskRecordCountResponse> getTaskRecordCount(Long id) {
        return new ResponseData<>().success(dozerUtil.convertor(taskQueryService.getTaskRecordCount(id), TaskRecordCountResponse.class));
    }

    @Override
    public ResponseData<List<TaskRecordResponse>> getQueryListTaskRecord(QueryListTaskRecordRequest queryListTaskRecordRequest) {
        return new ResponseData<>().success(dozerUtil.convertor(taskQueryService.queryListTaskRecord(queryListTaskRecordRequest), TaskRecordResponse.class));
    }

    @Override
    public ResponseData<PaginationData<TaskRecordResponse>> getQueryPageTaskRecord(QueryPageTaskRecordRequest queryPageTaskRecordRequest) {
        IPage<TaskRecordVo> taskRecordVoIPage = taskQueryService.queryPageTaskRecord(queryPageTaskRecordRequest);
        PaginationData<TaskRecordResponse> paginationData = dozerUtil.convertor(taskRecordVoIPage, PaginationData.class);
        paginationData.setRecords(dozerUtil.convertor(taskRecordVoIPage.getRecords(), TaskRecordResponse.class));
        return new ResponseData<>().success(paginationData);
    }

    @Override
    @SkipSqlPermission
    @SkipAuthority
    public ResponseData<Boolean>  postScheduleTask() {
        scheduleApplicationService.doSync();
        return new ResponseData<>().success(true);
    }

    @Override
    public ResponseData<Integer> updateTaskStatus() {
        int num = scheduleApplicationService.doTaskSync();
        return new ResponseData<>().success(num);
    }

    @Override
    public ResponseData<Boolean> postUpdateTaskRegularly() {
        boolean flag = taskApplicationService.updateTaskStatusRegularly();
        return new ResponseData<>().success(flag);
    }

    @Override
    public ResponseData<Boolean> postSafetyCheckWeekly() {
        boolean flag = taskApplicationService.safetyCheckWeekly();
        return new ResponseData<>().success(flag);
    }

    @Override
    public ResponseData<Boolean> postSafetyCheckMonthly() {
        boolean flag = taskApplicationService.safetyCheckMonthly();
        return new ResponseData<>().success(flag);
    }

    @Override
    public ResponseData<Boolean> postDeleteHistoryTask() {
        taskApplicationService.deleteHistoryTask();
        return new ResponseData<>().success(true);
    }

    @Override
    public ResponseData<Boolean> postCancelTask(TaskRecordRequest taskRecordRequest) {
        return new ResponseData<>().success(taskApplicationService.cancelTask(dozerUtil.convertor(taskRecordRequest, TaskRecordCommand.class)));
    }
}
